//
// UIColor+Extra.m
//
// Copyright (c) 2015 Zhao Xin (https://github.com/xinyzhao/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#import "UIColor+Extra.h"

UIColor* UIColorFromRGB(NSUInteger value) {
    CGFloat a = 1.f;
    if ((value & 0xff000000)) {
        a = ((value & 0xff000000) >> 24) / 255.f;
    }
    CGFloat r = ((value & 0x00ff0000) >> 16) / 255.f;
    CGFloat g = ((value & 0x0000ff00) >> 8) / 255.f;
    CGFloat b = (value & 0x000000ff) / 255.f;
    return [UIColor colorWithRed:r green:g blue:b alpha:a];
}

UIColor* UIColorFromHEX(NSString *string) {
    NSRange range = [string rangeOfString:@"[A-Fa-f0-9]{6,8}" options:NSRegularExpressionSearch];
    if (range.location != NSNotFound) {
        unsigned int hex = 0;
        NSString *str = [string substringWithRange:range];
        [[NSScanner scannerWithString:str] scanHexInt:&hex];
        return UIColorFromRGB(hex);
    }
    return [UIColor clearColor];
}

@implementation UIColor (Extra)

+ (instancetype)colorWithRGB:(NSUInteger)value {
    return UIColorFromRGB(value);
}

+ (instancetype)colorWithHEX:(NSString *)string {
    return UIColorFromHEX(string);
}

+ (UIColor *)colorWithHexStr:(NSString *)color alpha:(CGFloat)alpha
{
    //删除字符串中的空格
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6)
    {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6)
    {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}

+ (CAGradientLayer *)setGradualChangingColor:(CGRect)rect fromColor:(NSString *)fromHexColorStr toColor:(NSString *)toHexColorStr {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = rect; // 创建渐变色数组，需要转换为CGColor颜色
    gradientLayer.colors = @[
                             (__bridge id)[UIColor colorWithHEX:fromHexColorStr].CGColor,
                             (__bridge id)[UIColor colorWithHEX:toHexColorStr].CGColor];
    // 设置渐变颜色方向，左上点为(0,0), 右下点为(1,1)
    gradientLayer.startPoint = CGPointMake(1, 0);
    gradientLayer.endPoint   = CGPointMake(1, 1); // 设置颜色变化点，取值范围 0.0~1.0
    gradientLayer.locations  = @[@0,@1];
    return gradientLayer;
    
}

@end
